const {staticUrl} = require("../../services/config");

const app = getApp();
const formMixin = app._require("mixins/form.mixin.js").default;
const {AskPage} = app._require("common/index");
// const  projectMixin  = app._require("./projects-action.js").default;
// import { projectMixin } from "./projects-action.js";
const {projectMixin} = app._require("pages/projects-manager/projects-action.js");
AskPage({
    /**
     * 页面的初始数据
     */
    mixins: [formMixin, projectMixin],
    data: {
        needRequest: false,
        curentIndex: "",
        navBarHeight: app.globalData.navBarHeight,//导航栏高度
        errorImgs: {},
        items: [
            {label: "全部", value: "past", state: "1"},
            {label: "延迟", value: "expiring", state: "2"},
            {label: "正常", value: "zc", state: "3"},
            {label: "提前", value: "tq", state: "3"},
        ],
        itemsInfo: {0: "", 1: "", 2: "", 3: ""},
        imgUrls: [
            "/assets/project-manager/p_banner.png",
            "/assets/project-manager/p_banner.png",
            "/assets/project-manager/p_banner.png",
        ]
    },

    onShow: function () {
        if (typeof this.getTabBar === "function" && this.getTabBar()) {
            this.getTabBar().setData({
                active: "/" + this.route,
            });
        }
        this.setData({
            user: app.user,
        });

        let _change = app.user.clearManager;
        if (_change == true) {
            let now_type = this.data.now_type;
            this.requestAction(now_type);

            app.user.clearManager = false;
            this.loginFail();
            wx.setStorage({
                key: "user",
                data: app.user,
            });
        }

        if (app.user.token.length > 0) {
            let reolaValue = wx.getStorageSync("reolaValue");
            var currentIndex = this.data.currentIndex;
            console.log(reolaValue);
            if (reolaValue && currentIndex != undefined) {
                let records = this.data.records;
                let item = records[currentIndex];
                console.log(currentIndex);
                console.log(item);
                let vprocess = reolaValue.process;
                if (!vprocess) {
                    reolaValue.process = 0;
                }
                let type = reolaValue.stageCompleteStatus;

                item.process = reolaValue.process;
                item.stageProcess = reolaValue.stageProcess;
                item.stageCompleteStatus = reolaValue.stageCompleteStatus;
                try {
                    let resImg = reolaValue.resImg[0];
                    item.resImg = resImg;
                    item.url = staticUrl + "/" + resImg;
                } catch (e) {
                }
                var statue_name = "";
                if (type == 2) {
                    statue_name = "正常";
                    item.status_bg = "#01C690";
                    item.status_value = vprocess + "%";
                    item.name_title = "正常"
                    item.type = 2
                } else if (type == 3) {
                    item.status_bg = "#234BFC";
                    item.status_value = vprocess + "%";
                    statue_name = "提前";
                    item.name_title = "提前"
                    item.type = 3
                } else if (type == 1) {
                    item.status_bg = "#DD1818";
                    item.status_value = vprocess + "%";
                    statue_name = "延迟";
                    item.name_title = "延迟"
                    item.type = 1
                }

                //===> 需要特殊处理
                let now_type = this.data.now_type;
                // let itemsInfo = this.data.itemsInfo;
                if (now_type > 0 && now_type != type) {
                    //当状态发生变化
                    records.splice(currentIndex, 1);

                    // let total_num = itemsInfo[now_type];
                    try {
                        // total_num = parseInt(total_num);

                        // if (total_num > 0) {
                        //     total_num = total_num - 1;
                        // } else {
                        //     total_num = "";
                        // }
                        // console.log(total_num);
                        // itemsInfo[now_type] = total_num;
                    } catch (e) {
                        console.log(e);
                    }

                    wx.showToast({
                        title: "该任务状态已变成为" + statue_name,
                        duration: 3000,
                        icon: "none",
                    });
                }

                this.setData({
                    records: records,
                    // itemsInfo: itemsInfo,
                });
            }


            wx.setStorage({
                data: null,
                key: "reolaValue",
            });
        } else {
            this.loginFail();
        }
    },
    onLoad: function (options) {
        if (app.user.token.length > 0) {
            this.requestAction();
            // this.getProjectCount();
        } else {
            this.loginFail();
        }
    },
    loginFail() {
        //未登录
        this.setData({
            banner: [{path: "/assets/home/home_banner_one.jpg"}],
            records: [],
            itemsInfo: {0: "", 1: "", 2: "", 3: ""},
            isFinish: true, //全部加载完毕
        });
    },
    requestAction(type = "", page = 1) {
        console.log("请求的type==" + type + "请求的page==" + page);
        this.project_banner(type);
        this.projectList(type, page);

    },
    scrollReachBottom() {
        let type = this.data.now_type ?? "";
        let page = this.data.currentPage;
        let isFinish = this.data.isFinish;
        if (!isFinish) {
            this.requestAction(type, page + 1);
        }
    },
    clickToDetails(item) {
        wx.navigateTo({
            url: "../plugins/projects-manager-details/index?id=" + item.id,
        });
    },
    clickToProcess(e) {
        let item = e.currentTarget.dataset.item;
        let currentIndex = e.currentTarget.dataset.index;
        // console.log(e);
        wx.navigateTo({
            url:
                "../plugins/projects-manager/fill-process/index?id=" +
                item.id +
                "&collectorId=" +
                item.collectorId +
                "&name=" +
                item.name,
        });
        this.setData({
            currentIndex: currentIndex,
        });
        // this.data.curentIndex = currentIndex;

    },
    handleTabClick(e) {
        // console.log(e);
        // this.getProjectCount();
        let index = e.detail.index;
        let type = index == 0 ? "" : index;
        this.project_banner(type);
        this.projectList(type);
        this.setData({
            now_type: type,
        });
    },
    async project_banner(type = "") {
        if (app.user.token.length == 0) {
            this.loginFail();
            return;
        }

        try {
            const response = await this.$_server("project/swiper", {
                params: {
                    id: "?currentPage=" + "1" + "&pageSize=" + "8" + "&stageCompleteStatus=" + type,
                },
            });
            let {data} = response;

            if (data.code != 200) {
                wx.showToast({
                    title: data.message,
                    icon: "none",
                });
                this.setData({
                    banner: [{path: "/assets/home/home_banner_one.jpg"}],
                });
                return;
            }

            let banner = data.data;
            banner.forEach(element => {
                //  console.log(element);
                element.path = staticUrl + element.path;
            });

            this.setData({
                banner: banner.length == 0 ? [{path: "        /assets/home/home_banner_one.jpg"}] : banner,
            });
        } catch (e) {
            this.loginFail();
        }
    },
    /**
     * 状态
     * null 表示全部
     * 1 延迟
     * 2 正常
     * 3 提前
     */
    // private Integer type;
    async projectList(type = "", page = 1) {
        if (app.user.token.length == 0) {
            this.loginFail();
            return;
        }
        // console.log("======"+page);
        if (page == 1) {
            this.getProjectCount();
        }
        const response = await this.$_server("project/list", {
            params: {
                id: "?currentPage=" + page + "&pageSize=" + "10" + "&stageCompleteStatus=" + type,
            },
        });
        let {data} = response;

        if (data.code != 200) {
            wx.showToast({
                title: data.message,
                icon: "none",
            });
            this.loginFail();
            return;
        }
        this.setData({
            currentPage: page,
        });

        let records = data.data.records;
        if (page == 1) {
        } else {
            let new_records = this.data.records ?? [];
            //加到最后
            records = new_records.concat(records);
        }

        // let itemsInfo = this.data.itemsInfo;
        let key = type.length == 0 ? "0" : type;
        let total = data.data.total;
        // itemsInfo[key] = total > 0 ? total : "";

        records.forEach(element => {
            let process = element.process;
            let proportion = element.proportion;
            if (!process) {
                element.process = 0;
            }
            if (!proportion) {
                element.proportion = 0;
            }
            let type = element.stageCompleteStatus;

            if (type == 2) {
                element.status_bg = "#65A65B";
                element.status_value = process + "%";
                element.name_title = "正常"
                element.type = 2
            } else if (type == 3) {
                element.status_bg = "#FF8635";
                element.status_value = process + "%";
                element.name_title = "提前"
                element.type = 3
            } else if (type == 1) {
                element.status_bg = "#DD1818";
                element.status_value = process + "%";
                element.name_title = "延迟"
                element.type = 1
            }

            let resImg = element.resImg;
            if (resImg) {
                if (resImg.substring(0, 1) == "/") {
                    resImg = staticUrl + resImg;
                } else {
                    resImg = staticUrl + "/" + resImg;
                }

                element.url = resImg;
            }
        });
        this.setData({
            // itemsInfo: itemsInfo,
            records: records,
            isFinish: records.length >= data.data.total, //全部加载完毕
        });
        if (page >= 1) {
            wx.stopPullDownRefresh();
        }
    },
    errDetails(e) {
        // console.log(e);

        let errorImgs = this.data.errorImgs;
        var path = e.currentTarget.dataset.path;
        // errorImgs.push(path);
        errorImgs[path] = "1";
        this.setData({
            errorImgs: errorImgs,
        });
        // console.log(this.data.errorImgs);
    },
    clickShowBigImage(e) {
        let item = e.currentTarget.dataset.item;
        let url = item.url ? item.url : "";
        // if(resImg.length==0){return}
        wx.previewImage({
            urls: [url], //需要预览的图片http链接列表，注意是数组
            current: "", // 当前显示图片的http链接，默认是第一个
            success: function (res) {
            },
            fail: function (res) {
                console.log(res);
            },
            complete: function (res) {
            },
        });
        // console.log(item.url);
    },
    loginSuccess(e) {
        // console.log(e);
        console.log(app.user);
        this.setData({
            user: app.user,
        });
        //===>请求==request
        // this.onRefresh();
        this.requestAction(this.data.type, 1);
    },
    clickDetailsAction(e) {
        let item = e.currentTarget.dataset.item;
        let index = e.currentTarget.dataset.index;
        this.setData({
            currentIndex: index,
        });
        this.clickToDetails(item);
    },
});
